﻿using BLL;
using BookM.AddReviseform;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;

namespace BookM
{
    public partial class ReaderManagement : Form
    {
        public ReaderManagement()
        {
            InitializeComponent();
        }

        ReaderManagementBLL reader = new ReaderManagementBLL();


        private void ReaderManagement_Load(object sender, EventArgs e)
        {
            #region 转list
            //IList<ReaderInfo> resources = new List<ReaderInfo>();
            //ReaderInfo resource = new ReaderInfo();
            //resources.Add(resource);
            //Dgv_list.DataSource = resources;
            #endregion

            Dgv_list.DataSource = reader.ReaderShow();
            Dgv_list.AutoGenerateColumns = false;//不让datagridview自动生成列，可在属性中修改
            Dgv_list.AllowUserToAddRows = false;//禁止自动生成行可在属性中修改
        }

        /// <summary>
        /// 右键操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dgv_list_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    Dgv_list.ClearSelection();
                    Dgv_list.Rows[e.RowIndex].Selected = true;
                    Dgv_list.CurrentCell = Dgv_list.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    //MessageBox.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }


  

       
     
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (Dgv_list.Rows.Count > 0)
            {
                string strRow = Dgv_list.CurrentCell.Value.ToString();
                int index = Dgv_list.SelectedCells[0].RowIndex;
                var BookID = Dgv_list.Rows[index].Cells["读者姓名"].Value;
                var BorrowReturnID = Dgv_list.Rows[index].Cells["ID"].Value;
                if (BookID != null && BorrowReturnID != null)
                {
                    if (MessageBox.Show(" 确认要读者：‘" + BookID + "’ 吗？", " 删除 ", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        if (reader.DatateReader(BorrowReturnID.ToString()))
                        {
                            MessageBox.Show(EnumTools.GetDescription(ManageEnums.DOCUMENT.Datate_SUCCEED));
                            Dgv_list.DataSource = reader.ReaderShow();
                        }
                        else
                        {
                            MessageBox.Show(EnumTools.GetDescription(ManageEnums.DOCUMENT.Datate_LOSE));
                        }

                    }
                }
            }
        }
           

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 修改ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            int index = Dgv_list.SelectedCells[0].RowIndex;
            var BorrowReturnID = Dgv_list.Rows[index].Cells["ID"].Value;
            DialogResult dt = new ReaderUpdateForm(BorrowReturnID.ToString()).ShowDialog();
            if (dt == DialogResult.OK)
            {
                Dgv_list.DataSource = reader.ReaderShow();
            }
        }
    }
}
